package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private EmployeeMapper employeeMapper;

    // 处理查询所有员工请求
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        List<Department> departments = departmentService.listAll();
        // 查询所有部门往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("depts", departments);
        PageInfo<Employee> result = employeeService.query(qo);
        model.addAttribute("result", result);
        // 查询所有部门往模型存入，提供数据，让员工列表页面显示出来以供用户选择
        return "/employee/list";//找视图/WEB-INF/views/employee/list,jsp
    }

    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return new JsonResult(true, "操作成功");

    }

    //处理批量删除请求
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {

        if (ids != null && ids.length > 0) {
            employeeService.batchDelete(ids);
        }
        return new JsonResult(true, "删除成功");

    }

    // 去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model) {

        List<Department> departments = departmentService.listAll();
        // 查询所有部门往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("depts", departments);
        // 查询所有角色往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("roles", roleService.listAll());
        if (id != null) { // 表示去修改
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }

        return "employee/input";

    }

    //修改或新增
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() == null) {
            employeeService.save(employee, ids); // 新增
        } else {
            employeeService.update(employee, ids);//修改
        }
        return new JsonResult(true, "保存成功");//让浏览器重新发一次请求

    }

    /*
     * 检查用户名是否存在
     * 返回true:(验证通过)不存在
     * false:(验证不通过)已经存在
     * */
    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String name, Long id) {
        HashMap<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByName(name);
        if (id == null) {
            map.put("valid", employee == null);//添加
        }else {
            Employee employee1 = employeeService.get(id);//通过传入id获取对象
            map.put("valid",employee1.getName().equals(name) ||  employee == null );//编辑
        }
        return map;

    }


    /*
     * 员工导出
     * */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }


    /*
     * 员工导入
     * */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult(true, "导入成功");
    }


}

